package com.yuneec.fly.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import com.yuneec.fly.R;

public class SlideVernierView extends View {
    private static final int VIEW_WIDTH = 51;
    private static final int VIEW_HEIGHT = 139;
    private static final int SELECTED_WIDTH = 33;
//	private static final int SELECTED_HEIGHT = 16;

    Bitmap bitmap_bg = null;
    Bitmap bitmap = null;

    private Paint mPaint = new Paint();
    private int mValue;
    private float cX, cY;

    public SlideVernierView(Context context) {
        super(context);
        init(context);
    }

    public SlideVernierView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SlideVernierView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        bitmap_bg = BitmapFactory.decodeResource(getResources(), R.drawable.hwm_slide_vernier);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.hwm_slide_vernier_cursor);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        drawBackground(canvas);
        drawCursor(canvas);
    }

    public void drawBackground(Canvas canvas) {
        mPaint.setAntiAlias(true);
        canvas.drawBitmap(bitmap_bg, null, new Rect(0, 0, VIEW_WIDTH, VIEW_HEIGHT), mPaint);
    }

    public void drawCursor(Canvas canvas) {
        mPaint.setAntiAlias(true);
        cX = 9; //(VIEW_WIDTH - SELECTED_WIDTH)/2;
        cY = mValue * 0.615f;//((VIEW_HEIGHT - SELECTED_HEIGHT)/200);
        canvas.drawBitmap(bitmap, cX, cY, mPaint);
    }

    public void setValue(int value) {
        if (mValue != value) {
            if (mValue - value >= 5 || mValue - value <= -5) {
                mValue = value;
                invalidate();
            }
        }
    }
}
